// Package day_08_hash_hashmap random_pool
// @Title day_08_hash_hashmap
// @Description
// @Author  == 琳琳也叫王木木 ==  2021/9/2 15:58
// @Create  2021/9/2 15:58
package day_08_hash_hashmap

type MyRandomPool struct {
	KeyIndexMap map[string]int
	IndexKeyMap map[int]string
	KeySize     int
}

//func MyRandomPool() {
//
//}

func (this *MyRandomPool) insert(key string) {
	_, ok := this.KeyIndexMap[key]
	// 如果字符串不为空，且字符串在map中没有出现过
	if key != "" && len(key) > 0 && ok != true {
		this.KeySize++
		this.KeyIndexMap[key] = this.KeySize
		this.IndexKeyMap[this.KeySize] = key
	}
}

func (this *MyRandomPool) delete(key string) {
	_, ok := this.KeyIndexMap[key]
	if ok == true {
		deleteIndex := this.KeyIndexMap[key] // 获取要删除的下标
		this.KeySize--
		lastIndex := this.KeySize
		lastKey := this.IndexKeyMap[lastIndex]
		this.KeyIndexMap[lastKey] = deleteIndex
		this.IndexKeyMap[deleteIndex] = lastKey
		//this.KeyIndexMap.remove(key)
		//this.IndexKeyMap.remove(lastIndex)

	}
}

func (this *MyRandomPool) getRandom() string {
	if this.KeySize == 0 {
		return ""
	}
	//randomIndex := int(math.Round()*this.KeySize)
	randomIndex := 1
	return this.IndexKeyMap[randomIndex]
}
